package cn.youframe.frame.base;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * User: Administrator
 * DateTime: 12-6-24: 上午10:17
 */
public class BaseDaoImpl extends JdbcDaoSupport implements BaseDao{
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 查询对象
     * @param sql
     * @param requiredType
     * @param <T>
     * @return
     */
    @Override
    public <T> T queryObject(String sql, Class<T> requiredType) {
        return queryObject(sql, requiredType,null);
    }

    @Override
    public <T> T queryObject(String sql, Class<T> requiredType, Object[] args) {
        if(args==null || args.length==0){
            return getJdbcTemplate().queryForObject(sql,requiredType);
        }else {
            return getJdbcTemplate().queryForObject(sql,requiredType,args);
        }
    }

    /**
     * 查询列表
     * @param sql
     * @param requiredType
     * @param <T>
     * @return
     */
    @Override
    public <T> List<T> queryList(String sql, Class<T> requiredType) {
        return queryList(sql, requiredType,null);
    }

    @Override
    public <T> List<T> queryList(String sql, Class<T> requiredType, Object[] args) {
        if(args==null || args.length==0){
            return getJdbcTemplate().query(sql, BeanPropertyRowMapper.newInstance(requiredType));
        }else {
            return getJdbcTemplate().query(sql, BeanPropertyRowMapper.newInstance(requiredType),args);
        }
    }

    /**
     * 更新数据
     * @param sql
     * @return
     */
    @Override
    public int update(String sql) {
        return getJdbcTemplate().update(sql);
    }

    @Override
    public int update(String sql, Object[] args) {
        return getJdbcTemplate().update(sql,args);
    }

    @Override
    public int update(String sql, Object[] args, int[] argTypes) {
        return getJdbcTemplate().update(sql,args,argTypes);
    }

    /**
     * 获取自增列主键值（Oracle不适用）
     * @param sql
     * @param paras
     * @return
     */
    public String insertAndGetKey(final String sql,final Object[] paras){
        KeyHolder holder=new GeneratedKeyHolder();
        getJdbcTemplate().update(new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                PreparedStatement ps = con.prepareStatement(sql);
                if(paras!=null && paras.length>0){
                    int idx=1;
                    for (Object pobj:paras){
                        ps.setObject(idx,pobj);
                        idx++;
                    }
                }
                return ps;
            }
        }, holder);
        String key=String.valueOf(holder.getKey());
        return key;

    }
    /**
     * 根据列名获取插入后的值(适用于Oracle)
     * @param sql
     * @param paras
     * @param keyColumns
     * @return
     */
    public Map<String,Object> insertAndGetKey(final String sql,final Object[] paras,final String[] keyColumns){
        KeyHolder holder=new GeneratedKeyHolder();
        getJdbcTemplate().update(new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                PreparedStatement ps = con.prepareStatement(sql,keyColumns);
                if(paras!=null && paras.length>0){
                    int idx=1;
                    for (Object pobj:paras){
                        ps.setObject(idx,pobj);
                        idx++;
                    }
                }
                return ps;
            }
        }, holder);
        return holder.getKeys();
    }
    /**
     * 根据列名获取插入后的值
     * @param sql
     * @param keyColumns
     * @return
     */
    public Map<String,Object> insertAndGetKey(final String sql,final String[] keyColumns){
        return insertAndGetKey(sql,null,keyColumns);
    }
}
